<template>
  <div class="app-container">
      <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchMap.data" class="demo-form-inline">
      <el-form-item>
        <el-input
          v-model="searchMap.data.name"
          placeholder="分类名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchTable()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          @click="
            dialogFormVisible = true;
            contentCat = {};
          "
          >添加分类</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="delBatch()">批量删除</el-button>
      </el-form-item>
    </el-form>
      <!-- 表格 -->
    <el-table
      ref="multipleTable"
      :data="contentCatList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="id" label="分类ID" width="120"> </el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column prop="" label="操作">
          <template slot-scope="scope">
          <div>
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="editTable(scope.row)"
              >修改</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="block" >
      <el-pagination
      style="float: right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="searchMap.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>
    </div>
    <!-- 添加 -->
    <el-dialog
      :title="contentCat.id ? '修改分类' : '添加分类'"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
    >
      <el-form :model="contentCat">
        <el-form-item label="分类名称">
          <el-input v-model="contentCat.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveDate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {search,save,del} from "@/api/contentCat"
export default {
  data() {
    return {
      searchMap: {
        page: 1,
        limit: 5,
        data: {},
      },
      contentCatList:[],
      contentCat: {}, //实体
      dialogFormVisible:false,
      ids:[]
    };
  },
  methods: {
       // 搜索
    searchTable() {
      var _this = this;
      // if(this.searchMap.data.name=""){
      //   this.searchMap.data={};
      // }
      var searchMap1= {
        page: 1,
        limit: 5,
        data: {},
      };
      if (this.searchMap.data.name==""){
        search(searchMap1).then((res) => {
        _this.contentCatList = res.data;
        _this.total = res.total;
      });
      }else{
         search(this.searchMap).then((res) => {
        _this.contentCatList = res.data;
        _this.total = res.total;
      });
      }
     
    },
    //修改
    editTable(row){
        this.dialogFormVisible=true
        this.contentCat=row
    },
    //保存
    saveDate() {
      save(this.contentCat).then((res) => {
        if (res.success) {
          this.searchTable();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
        this.dialogFormVisible = false;
      });
    },
    //批量删除
    handleSelectionChange(val) {
        var _this=this
      this.contentCat = val;
      this.contentCat.forEach(item => {
          _this.ids.push(item.id)
      });
    },
    delBatch() {
      del(this.ids).then((res) => {
        if (res.success) {
          this.searchTable();
          this.$message({
            showClose: true,
            message: res.msg,
            type: "success",
          });
        } else {
          this.$message({
            showClose: true,
            message: res.msg,
            type: "error",
          });
        }
      });
    },
    handleSizeChange(size) {
      this.searchMap.limit = size;
      this.searchTable();
    },
    handleCurrentChange(currentPage) {
      this.searchMap.page = currentPage;
      this.searchTable();
    },
  },
  created() {
      this.searchTable()
  },
};
</script>